package com.yc.yyds.servlet;

import com.google.gson.Gson;
import com.yc.yyds.Bean.Result;
import com.yc.yyds.dao.CinemaDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveCinemaServlet", value = "/SaveCinemaServlet.s")
public class SaveCinemaServlet extends HttpServlet {
    CinemaDao dao=new CinemaDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cinemaname = request.getParameter("cinemaname");
        String cinemadesc = request.getParameter("cinemadesc");
        String seats = request.getParameter("seats");
        String cinemaid = request.getParameter("cinemaid");
        Result result;
        try{
            if(cinemaid!=null){
                dao.modify(cinemaid,cinemaname,cinemadesc,seats);
            }else{
                dao.create(cinemaname,cinemadesc,seats);
            }
            result=new Result(1,"保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            result=new Result(0,e.getMessage(),null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);

    }
}
